<template>
    <el-container class="home_container">
      <!-- 头部区域 -->
      <el-header>
        <div class="header_right">
          <img src="../../assets/heima.png" alt="">
          <span class="title">电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主题区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
            <div class="toggle_button" @click="toggleCollapse">|||</div>
              <el-menu
                background-color="#333744"
                text-color="#fff"
                active-text-color="#409EFF"
                unique-opened
                :collapse="isCollapse"
                :collapse-transition="false"
                router
                :default-active="activePath">
                <!-- collapse是否水平折叠收起菜单仅在 mode 为 vertical 时可用 -->
                <!-- collapse-transition 是否开启折叠动画 -->
                <!-- :unique-opened="true" 只展示一个值列表 -->
                <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                    <template slot="title">
                      <i :class="'iconfont '+iconsObj[item.id]"></i>
                      <span>{{ item.authName }}</span>
                    </template>
                    <el-menu-item :index="'/' + subitem.path"  v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
                      <template slot="title">
                        <i class="el-icon-menu"></i>
                        <span>{{ subitem.authName }}</span>
                      </template>
                    </el-menu-item>
                </el-submenu>
              </el-menu>
        </el-aside>
        <!-- 右侧内容 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      menulist: [],
      iconsObj: {
        125: 'icon-user',
        103: 'icon-tijikongjian',
        101: 'icon-shangpin',
        102: 'icon-danju',
        145: 'icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      //
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    toggleCollapse() {
      // 点击按钮切换侧边栏的展开和收起
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
  .home_container{
    height: 100%;
  }
  .el-header{
    background-color: #373D41;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .header_right{
      display: flex;
      align-items: center;
      img{
        margin-right: 15px;
      }
      .title{
        font-size: 22px;
        color: #FFF;
      }
    }
  }
  .el-menu{
    border-right: none;
  }
  .el-aside{
    background-color: #333744;
  }
  .el-main{
    background-color: #EAEDF1;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle_button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #FFFFFF;
    text-align: center;
    letter-spacing: .12em;
    cursor: pointer;
  }
</style>
